package com.jiumi.order.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.jiumi.common.annotation.DataScope;
import com.jiumi.common.utils.StringUtils;
import com.jiumi.order.domain.BaseOrderPayInfo;
import com.jiumi.order.service.IBaseOrderPayInfoService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jiumi.common.annotation.Log;
import com.jiumi.common.core.controller.BaseController;
import com.jiumi.common.core.domain.AjaxResult;
import com.jiumi.common.enums.BusinessType;
import com.jiumi.order.domain.BaseOrderInfo;
import com.jiumi.order.service.IBaseOrderInfoService;
import com.jiumi.common.utils.poi.ExcelUtil;
import com.jiumi.common.core.page.TableDataInfo;

/**
 * 订单信息Controller
 *
 * @author jiumi
 * @date 2023-05-24
 */
@RestController
@RequestMapping("/order/BaseOrderInfo")
public class BaseOrderInfoController extends BaseController
{
    @Autowired
    private IBaseOrderInfoService baseOrderInfoService;

    @Autowired
    private IBaseOrderPayInfoService baseOrderPayInfoService;
    /**
     * 查询订单信息列表
     */
    @PreAuthorize("@ss.hasPermi('order:BaseOrderInfo:list')")
    @GetMapping("/list")
    @DataScope(userAlias = "u")
    public TableDataInfo list(BaseOrderInfo baseOrderInfo)
    {
        startPage();
        List<BaseOrderInfo> list = baseOrderInfoService.selectBaseOrderInfoList(baseOrderInfo);
        return getDataTable(list);
    }

    /**
     * 导出订单信息列表
     */
    @PreAuthorize("@ss.hasPermi('order:BaseOrderInfo:export')")
    @Log(title = "订单信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BaseOrderInfo baseOrderInfo)
    {
        List<BaseOrderInfo> list = baseOrderInfoService.selectBaseOrderInfoList(baseOrderInfo);
        ExcelUtil<BaseOrderInfo> util = new ExcelUtil<BaseOrderInfo>(BaseOrderInfo.class);
        util.exportExcel(response, list, "订单信息数据");
    }

    /**
     * 获取订单信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('order:BaseOrderInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(baseOrderInfoService.selectBaseOrderInfoById(id));
    }

    /**
     * 新增订单信息
     */
    @PreAuthorize("@ss.hasPermi('order:BaseOrderInfo:add')")
    @Log(title = "订单信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseOrderInfo baseOrderInfo)
    {


        return toAjax(baseOrderInfoService.insertBaseOrderInfo(baseOrderInfo));
    }

    /**
     * 修改订单信息
     */
    @PreAuthorize("@ss.hasPermi('order:BaseOrderInfo:edit')")
    @Log(title = "订单信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseOrderInfo baseOrderInfo)
    {
        return toAjax(baseOrderInfoService.updateBaseOrderInfo(baseOrderInfo));
    }

    /**
     * 删除订单信息
     */
    @PreAuthorize("@ss.hasPermi('order:BaseOrderInfo:remove')")
    @Log(title = "订单信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long ids)
    {
        BaseOrderPayInfo param=new BaseOrderPayInfo();
        param.setOrderId(ids);
        List<BaseOrderPayInfo> payList=  baseOrderPayInfoService.selectBaseOrderPayInfoList(param);
        if(payList.size()>0){
            return AjaxResult.error("存在收款订单，不允许删除");
        }
        return toAjax(baseOrderInfoService.deleteBaseOrderInfoById(ids));
    }
}
